<?php
include_once "../../qc_base.php";
include_once "../../qc_utils.php";
include_once "../../bootstrap.php";

include_once "../../classes/MakePage.php";


/**
   * This file contains the class
   * @package phpnewmaker
   */
   


function makesuredir($dir){
	if(!file_exists(dirname($dir))){
		makesuredir(dirname($dir));
	}
	if(!file_exists($dir)){
		mkdir($dir);
	}
}


function write_pdo_class_to_file($crudset_id){

	$qcs = Doctrine_Query::create()
	->from('Crudsets cs')->where('crudset_id = ?',$crudset_id);
	$setinfo = $qcs->fetchOne();


	// get the main table for the set
	$qtb = Doctrine_Query::create()
	->from('Tablesources tb')->where('tablesource_id = ?',$setinfo['main_table']);
	$tablesource = $qtb->fetchOne();
	$table_name = $tablesource['table_name'];







	$q = Doctrine_Query::create()
	->from('Projects p')->where('project_id = ?',$setinfo['project_id']);
	$project = $q->execute()->getFirst();	
	
	$project_db = $project->project_db;
	$project_path = $project->project_path;
	$project_server = $project->project_server;
	$project_login = $project->project_login;
	$project_pass = $project->project_pass;
   	$project_name = $project->project_name;
	$class_subpath = $project->class_subpath;
	$template_dir = $project->template_dir;	
	$structure = $project->file_structure;




	$structure_path = QC_SYS_BASEDIR . '/structures/';
	
	include $structure_path . $structure .'/paths.php';
	

	$outpath = $paths['modules'];
	$outpath = str_replace('<setname>',$setinfo['cs_name'],$outpath);
	
	$outpath = QC_SYS_BASEDIR . '/' . QC_PROJECT_SUBPATH . $project_name . '/' . $outpath ;

	makesuredir($outpath);
	
	
	$targetfile = $outpath . $setinfo['cs_name'] . '_class.php';
	$tplfile = 'pdo_class_tpl.php';

//--------------------Create Replacement Text

$ArrayClass = '
class ' . $setinfo['cs_name'] . 'Array extends CF_ArrayBySpec {
';
$ClassClose = '
}
';
$FilterClass = '
class ' . $setinfo['cs_name'] . 'Filter extends CF_FilterQueryBase {
';
$LookupClass = '
class ' . $setinfo['cs_name'] . 'LookupList extends CF_LookupList {
';

$ListClass = '
class ' . $setinfo['cs_name'] . 'DataLister extends CF_DataListerPDO{
	
	function GetDataList(){
		return parent::GetDataList("SELECT * FROM ' . $table_name . ' WHERE 1 ");
	}
';

// Create lookup for all columns that have edit_type = 'querylist'
	$q = Doctrine_Query::create()
	->from('Crudcols cc')->where('crudset_id = ?',$crudset_id);
	$list_array = $q->fetchArray();
	
	foreach ($list_array as $rowkey => $currentrow){
		$variable_id = $currentrow['variable_id'];
		//print 'Getting Variable for id ' . $currentrow['variable_id'];
		$qv = Doctrine_Query::create()
		->from('Variables v')->where('variable_id = ?',$variable_id);
		$varsrecord = $qv->fetchOne();
		$variable_name = $varsrecord['variable_name'];	

print 'Checking column ' . $variable_name . " num {$currentrow['crudcol_id']}Is " . $currentrow['edit_type'] . "\n";
		if($currentrow['edit_type'] == 'querylist'){
		
			$LookupMethods .= '
	function lookup_' . $variable_name . '(){
		$qry_lookup = "' . $currentrow['list_query'] . '";
		global $dbh;
		$stmt = $dbh->prepare($qry_lookup);	

		$ar_lookup = get_array_stmt($stmt);	
		return $ar_lookup;
	}
	
';
		}
		else if ($currentrow['edit_type'] == 'querylist'){
			$LookupMethods .= '
		function lookup_' . $variable_name . '(){
			// Write function to make array out of this
		$list_string = $currentrow[\'list_query\'];
        $commalist_array = explode(\',\',$list_string);
		}
';
		}

	}
//---------------------------Write into file	
	
	

	$pagemaker = new MakePage();

//	$pagemaker->AddReplacement('includefieldspec',$includefieldspec);
	$pagemaker->AddReplacement('setcsname',$setcsname);	
	$pagemaker->AddReplacement('ArrayClass',$ArrayClass);
	$pagemaker->AddReplacement('ClassClose',$ClassClose);
	$pagemaker->AddReplacement('FilterClass',$FilterClass);
	$pagemaker->AddReplacement('LookupClass',$LookupClass);
	$pagemaker->AddReplacement('ListClass',$ListClass);
	$pagemaker->AddReplacement('LookupMethods',$LookupMethods);	
	
	$newstuff = $pagemaker->ReplaceInFile($tplfile,$targetfile);
	
	$pagemaker->PrintReplacementsTable();

	echo '<pre>' . $pagemaker->str_log . '</pre>';
	
	file_put_contents($targetfile,$newstuff);	

	return $newstuff;


}
?>
